package com.hsfw.pmauthzuul.exception;

import com.hsfw.dbmodule.utils.BaseResponse;
import com.hsfw.dbmodule.utils.ResponseCode;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Created by RAZR on 16:37 2019/3/22.
 */
@RestControllerAdvice
public class DefaultExceptionHandler {
    /**
     * 处理所有自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public BaseResponse handleCustomException(CustomException e){
        return e.getBaseResponse();
    }
    /**
     * 处理参数校验异常
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public BaseResponse handleMethodArgumentNotValidException(MethodArgumentNotValidException e){
        return BaseResponse.buildResponse().setCode(ResponseCode.BAD_REQUEST).
                setMessage(e.getBindingResult().getFieldError().getDefaultMessage()).build();
    }
}
